from django.contrib.auth.models import User
from django.db import models
from thumbs import ImageWithThumbsField

#Modelo para los usuarios del sitio, este modelo extiende el modelo de usuarios de Django
class SiteUser(User):
    ImageWithThumbsField(upload_to='images/avatars', sizes=((200,200))).contribute_to_class(User,'avatar')
    models.CharField(max_length=20,help_text=("phone")).contribute_to_class(User,'phone')
    #models.IntegerField().contribute_to_class(User, 'ORIGEN')

class Intereses(models.Model):
    id_intereses = models.IntegerField(primary_key=True, db_column='ID_INTERESES') # Field name made lowercase.
    nombre_interes = models.CharField(max_length=300, db_column='NOMBRE_INTERES') # Field name made lowercase.

    class Meta:
        db_table = u'INTERESES'

class UsuFkInt(models.Model):
    id_intereses = models.ForeignKey(Intereses, primary_key=True, db_column='ID_INTERESES') # Field name made lowercase.
    id_usuario = models.ForeignKey(User, db_column='ID_USUARIO') # Field name made lowercase.
    class Meta:
        db_table = u'USU_FK_INT'

class Lugares(models.Model):
    id_lugar = models.CharField(max_length=10, primary_key=True, db_column='ID_LUGAR') # Field name made lowercase.
    lug_id_lugar = models.ForeignKey('self', null=True, db_column='LUG_ID_LUGAR', blank=True) # Field name made lowercase.
    nombre_lugar = models.CharField(max_length=300, db_column='NOMBRE_LUGAR') # Field name made lowercase.
    tipo_lugar = models.CharField(max_length=300, db_column='TIPO_LUGAR') # Field name made lowercase.

    class Meta:
        db_table = u'LUGARES'

class Publicaciones(models.Model):
    id_publicacion = models.IntegerField(primary_key=True, db_column='ID_PUBLICACION') # Field name made lowercase.
    id_usuario = models.ForeignKey(User, db_column='ID_USUARIO') # Field name made lowercase.
    fecha_publicacion = models.DateField(db_column='FECHA_PUBLICACION') # Field name made lowercase.
    resumen_publicacion = models.CharField(max_length=600, db_column='RESUMEN_PUBLICACION') # Field name made lowercase.
    estado_publicacion = models.IntegerField(db_column='ESTADO_PUBLICACION') # Field name made lowercase.
    contenido = models.CharField(max_length=30000, db_column='CONTENIDO') # Field name made lowercase.
    titulo = models.CharField(max_length=150, db_column='TITULO') # Field name made lowercase.
    hits = models.IntegerField(db_column='HITS')

    @classmethod
    def create_article(cls, title):
        publicacion = cls(titulo=title)
        publicacion.id_publicacion=get_nextautoincrement(Publicaciones)
        return publicacion

    def getTags(self):

        related_tags=PubFkTag.objects.filter(id_publicacion=self.id_publicacion)

        tagString=""
        for tags in related_tags:
            tag=Tags.objects.get(id_tag=tags.id_tag.id_tag)
            tagString+=tag.nombre_tag+","

        return tagString

    def comments_count(self):

        comments=Comentarios.objects.filter(id_publicacion=self.id_publicacion)

        return comments.count()


    class Meta:
        db_table = u'PUBLICACIONES'

class Tags(models.Model):
    id_tag = models.IntegerField(primary_key=True, db_column='ID_TAG') # Field name made lowercase.
    nombre_tag = models.CharField(max_length=90, db_column='NOMBRE_TAG') # Field name made lowercase.

    @classmethod
    def create_tag(cls, tag):
        tag = cls(nombre_tag=tag)
        tag.id_tag=get_nextautoincrement(Tags)
        return tag

    class Meta:
        db_table = u'TAGS'

class PubFkTag(models.Model):
    id_tag = models.ForeignKey(Tags, primary_key=True, db_column='ID_TAG') # Field name made lowercase.
    id_publicacion = models.ForeignKey(Publicaciones, db_column='ID_PUBLICACION') # Field name made lowercase.

    @classmethod
    def bind_tag(cls, pub, tag):
        pub_fk_tag = cls(id_publicacion=pub, id_tag=tag)
        pub_fk_tag.save()

    class Meta:
        db_table = u'PUB__FK_TAG'

class Multimedia(models.Model):
    id_multimedia = models.IntegerField(primary_key=True, db_column='ID_MULTIMEDIA') # Field name made lowercase.
    nombre_multimedia = models.CharField(max_length=60, db_column='NOMBRE_MULTIMEDIA') # Field name made lowercase.

    class Meta:
        db_table = u'MULTIMEDIA'

class PubFkMul(models.Model):
    id_multimedia = models.ForeignKey(Multimedia, primary_key=True, db_column='ID_MULTIMEDIA') # Field name made lowercase.
    id_publicacion = models.ForeignKey(Publicaciones, db_column='ID_PUBLICACION') # Field name made lowercase.
    class Meta:
        db_table = u'PUB_FK_MUL'

class Comentarios(models.Model):
    id_publicacion = models.IntegerField(db_column='ID_PUBLICACION') # Field name made lowercase.
    id_usuario = models.ForeignKey(User, db_column='ID_USUARIO') # Field name made lowercase.
    comentarios = models.CharField(max_length=15000, db_column='COMENTARIOS') # Field name made lowercase.

    class Meta:
        db_table = u'COMENTARIOS'

class Foros(models.Model):
    id_foro = models.IntegerField(primary_key=True, db_column='ID_FORO') # Field name made lowercase.
    id_usuario = models.ForeignKey(User, db_column='ID_USUARIO') # Field name made lowercase.
    titulo_foro = models.CharField(max_length=300, db_column='TITULO_FORO') # Field name made lowercase.
    descripcion=models.CharField(max_length=900, db_column='DESCRIPCION')
    estado_foro = models.IntegerField(db_column='ESTADO_FORO') # Field name made lowercase.
    fecha_apertura = models.DateField(db_column='FECHA_APERTURA') # Field name made lowercase.
    fecha_cierre = models.DateField(null=True, db_column='FECHA_CIERRE', blank=True) # Field name made lowercase.

    @classmethod
    def create_foro(cls, title):
        foro = cls(titulo_foro=title)
        foro.id_foro=get_nextautoincrement(Foros)
        return foro

    def contribs_count(self):
        contribs=Aportes.objects.filter(id_foro=self.id_foro)
        return contribs.count()

    class Meta:
        db_table = u'FOROS'

class Aportes(models.Model):
    id_usuario = models.ForeignKey(User, db_column='ID_USUARIO') # Field name made lowercase.
    id_foro = models.ForeignKey(Foros, db_column='ID_FORO') # Field name made lowercase.
    contenido_aporte = models.CharField(max_length=15000, db_column='CONTENIDO_APORTE') # Field name made lowercase.
    titulo_aporte = models.CharField(max_length=150, db_column='TITULO_APORTE') # Field name made lowercase.

    class Meta:
        db_table = u'APORTES'

class Notificaciones(models.Model):
    id_notificacion = models.IntegerField(primary_key=True, db_column='ID_NOTIFICACION')
    id_foro = models.ForeignKey(Foros, db_column='ID_FORO') # Field name made lowercase.
    id_usuario = models.ForeignKey(User, db_column='ID_USUARIO') # Field name made lowercase.
    descripcion_notificacion = models.CharField(max_length=900, db_column='DESCRIPCION_NOTIFICACION') # Field name made lowercase.
    fecha_notificacion = models.DateField(db_column='FECHA_NOTIFICACION') # Field name made lowercase.
    estado_notificacion = models.IntegerField(db_column='ESTADO_NOTIFICACION') # Field name made lowercase.

    class Meta:
        db_table = u'NOTIFICACIONES'


#Metodo para obtener el siguiente indice en campos autoincrementales.
def get_nextautoincrement(model):
    from django.db import connection
    cursor = connection.cursor()
    cursor.execute( "SELECT Auto_increment FROM information_schema.tables WHERE table_name='%s';" % \
    model._meta.db_table)
    row = cursor.fetchone()
    cursor.close()
    return row[0]